# -*- coding: utf-8 -*-
# @Time    : 2023/6/2 22:18
# @Author  : hy
# @Email   : 1596096369@qq.com
# @File    : tool.py
# @Software: PyCharm
from PIL import Image
import pytesseract
import cv2

class Swipe:
    def __init__(self,driver,size):
        self.driver = driver
        self.x = size.get("width")
        self.y = size.get("height")

    def swipe_up(self,duration=1500):
        start_point = (self.x*0.5,self.y*0.9)
        end_point = (self.x*0.5,self.y*0.1)
        self.driver.swipe(*start_point,*end_point,duration=duration)

    def swipe_down(self,duration=1500):
        start_point = (self.x*0.5,self.y*0.1)
        end_point = (self.x*0.5,self.y*0.9)
        self.driver.swipe(*start_point,*end_point,duration=duration)

    def swipe_left(self,duration=1500):
        start_point = (self.x*0.9,self.y*0.5)
        end_point = (self.x*0.1,self.y*0.5)
        self.driver.swipe(*start_point,*end_point,duration=duration)

    def swipe_right(self,duration=1500):
        start_point = (self.x*0.1,self.y*0.5)
        end_point = (self.x*0.9,self.y*0.5)
        self.driver.swipe(*start_point,*end_point,duration=duration)


def gen_img(base64_string, img):
    with open(img, 'wb') as f:
        f.write(base64_string)


def get_text(img):
    pytesseract.pytesseract.tesseract_cmd = r'D:\teeseract\tesseract.exe'
    # config = '--psm 6 -c tessedit_char_whitelist=0123456789'
    config = ""
    image = Image.open(img)
    text = pytesseract.image_to_string(image, config=config)
    return text


def get_text_from_cv2(img):
    image = cv2.imread(img)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, binary_image = cv2.threshold(gray_image, 175, 255, cv2.THRESH_BINARY)
    pytesseract.pytesseract.tesseract_cmd = r'D:\teeseract\tesseract.exe'
    text = pytesseract.image_to_string(binary_image)
    cv2.imwrite("kkk.png", binary_image)
    return text


def get_number_from_string(text):
    number = ""
    for i in text:
        if i in "0123456789":
            number += i
    return int(number)

if __name__ == '__main__':

    text = get_text_from_cv2('zan1.png')
    print(text)
    print(get_number_from_string(text))